﻿using Newtonsoft.Json;
using SaiPuLSCoffee.model;
using System;
using System.IO;
using System.Threading;
using System.Windows;

namespace SaiPuLSCoffee
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    //IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.cfInit1 = porPortInfo.cfInit1;
                    IPorPortInfo.Instance.cfInit2 = porPortInfo.cfInit2;
                    IPorPortInfo.Instance.cfcxInit1 = porPortInfo.cfcxInit1;
                    IPorPortInfo.Instance.cfcxInit2 = porPortInfo.cfcxInit2;
                    IPorPortInfo.Instance.JBInit1 = porPortInfo.JBInit1;
                    IPorPortInfo.Instance.JBInit2 = porPortInfo.JBInit2;
                    IPorPortInfo.Instance.CoffeeJB = porPortInfo.CoffeeJB;
                }
                Thread thr = new Thread(SocketServer.Instance.CoffeeMake);
                thr.IsBackground = true;
                thr.Start();
                Thread thr1 = new Thread(IOMsg.Instance.IOSerial_Thr);
                thr1.IsBackground = true;
                thr1.Start();
                Thread thr2 = new Thread(Robot1Msg.Instance.IOSerial_Thr);
                thr2.IsBackground = true;
                thr2.Start();
                Thread thr3 = new Thread(Robot2Msg.Instance.IOSerial_Thr);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr4 = new Thread(IceMakingMsg.Instance.IOSerial_Thr);
                thr4.IsBackground = true;
                thr4.Start();
            }
            catch (Exception)
            {
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                WuliaoCoffeeInit wuliao = new WuliaoCoffeeInit();
                wuliao.BeanCodeInit = 1;
                wuliao.ClassCodeInit = Convert.ToInt32(cmb.Text);
                wuliao.LeachInit = 0;
                wuliao.Guid = Guid.NewGuid().ToString();
                CoffeeMakes.Instance.coffeeInits.Add(wuliao);
            }
            catch (Exception)
            {

            }
        }
    }
}
